<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="" prop="productGranularity">
        <el-input
          v-model="queryParams.productGranularity"
          placeholder="请输入颗粒度对象"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="" prop="purchaseOrderNumber">
        <el-input
          v-model="queryParams.purchaseOrderNumber"
          placeholder="请输入采购单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="brand">
        <el-input
          v-model="queryParams.brand"
          placeholder="请输入品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="" prop="warehousedQty">
        <el-input
          v-model="queryParams.warehousedQty"
          placeholder="请输入已入仓"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="allocatedQty">
        <el-input
          v-model="queryParams.allocatedQty"
          placeholder="请输入已分配"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="unassignedQty">
        <el-input
          v-model="queryParams.unassignedQty"
          placeholder="请输入待分配"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="orderQty">
        <el-input
          v-model="queryParams.orderQty"
          placeholder="请输入下单数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入供应商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="" prop="purchaseAmount">
        <el-input
          v-model="queryParams.purchaseAmount"
          placeholder="请输入采购金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="在途数量" prop="inTransitQuantity">
        <el-input
          v-model="queryParams.inTransitQuantity"
          placeholder="请输入在途数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="差异数量" prop="differenceCount">
        <el-input
          v-model="queryParams.differenceCount"
          placeholder="请输入差异数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="" prop="sku">
        <el-input
          v-model="queryParams.sku"
          placeholder="请输入sku"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="skc">
        <el-input
          v-model="queryParams.skc"
          placeholder="请输入skc"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="spu">
        <el-input
          v-model="queryParams.spu"
          placeholder="请输入spu"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="item">
        <el-input
          v-model="queryParams.item"
          placeholder="请输入item"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="isNew">
        <!-- <el-input
          v-model="queryParams.isNew"
          placeholder="请输入是否新品"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select
          v-model="queryParams.isNew"
          placeholder="请输入是否新品"
          filterable
        >
          <el-option
            v-for="dict in dict.type.sys_order_isnew"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="isNew">
        <!-- <el-input
          v-model="queryParams.isNew"
          placeholder="请输入是否新品"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select
          v-model="queryParams.orderStatus"
          placeholder="订单状态"
          filterable
        >
          <el-option
            v-for="dict in dict.type.sys_order_orderstatus"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建日期" prop="orderNumber">
        <el-date-picker
          v-model="value2"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
          @change="changeTime"
          size="mini"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
      <!-- <br />
      <el-form-item label="创建时间:" label-width="80px">
        <timeSearch
          @getDateArr="getDateArr"
          @changDay="changDay"
          :timeArr="timeArr"
          :dayActive="this.dayActive"
          :dayLimit="1095"
        />
      </el-form-item> -->
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['productAllocation:trigger:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['productAllocation:trigger:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['productAllocation:trigger:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['productAllocation:trigger:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="handleRefresh"
          >同步数据</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="opentagkey"
          >手工分配</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button plain size="mini" @click="handlepurchaseOrderNumber"
          >选择采购单号</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row> -->
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="6" :xs="24">
        <el-card>
          <div
            slot="header"
            style="display: flex; justify-content: space-between"
          >
            <span> 采购单号 </span>
            <el-select
              v-model="queryParamsI.purchaseOrderNumber"
              placeholder="模糊查询"
              filterable
              size="mini"
              @focus="remoteselectOrderNumber"
              @change="getselectOrderNumber"
            >
              <el-option
                v-for="(dict, i) in AllList"
                :key="i"
                :label="dict"
                :value="dict"
              />
            </el-select>
          </div>
          <!-- @change="getselectOrderNumber" -->
          <el-table
            :data="purchaseDocumentCodeList"
            @cell-click="tableEdit"
            :height="500"
          >
            <el-table-column label="采购单号" align="center" prop="label" />
            <el-table-column
              label="是否选择"
              align="center"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handlepurchaseDocumentCode(scope.row)"
                  >确认</el-button
                >
              </template>
            </el-table-column>
          </el-table>

          <pagination
            small
            v-show="queryParamsItotal > 0"
            :total="queryParamsItotal"
            :page.sync="queryParamsI.pageNum"
            :limit.sync="queryParamsI.pageSize"
            @pagination="getselectOrderNumber"
          />
        </el-card>
      </el-col>

      <el-col :span="18" :xs="24">
        <el-card>
          <div
            slot="header"
            style="display: flex; justify-content: space-between"
          >
            <span> 采购单号明细 </span>
          </div>
          <el-table
            v-loading="loading"
            :data="triggerList"
            @selection-change="handleSelectionChange"
            :height="630"
          >
            <!-- <el-table-column type="selection" width="55" align="center" /> -->
            <!-- <el-table-column label="id" align="center" prop="id" /> -->
            <el-table-column
              label="颗粒度对象"
              align="center"
              prop="productGranularity"
            />
            <el-table-column
              label="订单状态"
              align="center"
              prop="orderStatus"
            />
            <el-table-column
              label="采购单号"
              align="center"
              prop="purchaseOrderNumber"
            />
            <el-table-column label="订单类型" align="center" prop="orderType" />
            <el-table-column label="品牌" align="center" prop="brand" />
            <el-table-column
              label="已入仓"
              align="center"
              prop="warehousedQty"
            />
            <el-table-column label="已分配" align="center" prop="allocatedQty">
              <template slot-scope="scope">
                <span
                  slot="reference"
                  style="color: #409eff; cursor: pointer"
                  size="mini"
                  @click="getBiCommentSecondDwdList(scope.row)"
                  >{{ scope.row.allocatedQty }}</span
                >
              </template>
            </el-table-column>
            <el-table-column
              label="待分配"
              align="center"
              prop="unassignedQty"
            />
            <el-table-column label="下单数量" align="center" prop="orderQty" />
            <el-table-column
              label="供应商"
              align="center"
              prop="supplierName"
            />
            <el-table-column
              label="采购金额"
              align="center"
              prop="purchaseAmount"
            />
            <el-table-column
              label="在途数量"
              align="center"
              prop="inTransitQuantity"
            />
            <el-table-column
              label="差异数量"
              align="center"
              prop="differenceCount"
            >
              <template slot-scope="scope">
                {{ scope.row.differenceCount }}
                <dict-tag
                  :options="dict.type.sys_product_arg"
                  :value="scope.row.stockInStatus"
                />
                <!-- <el-badge
            v-if="scope.row.differenceCount < 0"
            :value="'超'"
            class="item"
          ></el-badge>
          <el-badge
            v-if="scope.row.differenceCount > 0"
            :value="'缺'"
            class="item"
          ></el-badge> -->
              </template>
            </el-table-column>
            <el-table-column label="sku" align="center" prop="sku">
            </el-table-column>
            <el-table-column label="skc" align="center" prop="skc" />
            <el-table-column label="spu" align="center" prop="spu" />
            <el-table-column label="item" align="center" prop="item" />
            <el-table-column label="是否新品" align="center" prop="isNew">
              <template slot-scope="scope">
                <dict-tag
                  :options="dict.type.sys_order_isnew"
                  :value="scope.row.isNew"
                />
              </template>
            </el-table-column>
            <el-table-column
              label="采购时间"
              align="center"
              prop="purchaseDate"
            >
              <template slot-scope="scope">
                <span>{{
                  parseTime(scope.row.purchaseDate, "{y}-{m}-{d}")
                }}</span>
              </template>
            </el-table-column>
            <el-table-column
              label="操作"
              align="center"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['productAllocation:trigger:edit']"
                  >修改</el-button
                >
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['productAllocation:trigger:remove']"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-card>
      </el-col>
    </el-row>

    <!-- 添加或修改采购分配触发对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="颗粒度对象" prop="productGranularity">
          <el-input
            v-model="form.productGranularity"
            placeholder="请输入颗粒度对象"
          />
        </el-form-item>
        <el-form-item label="采购单号" prop="purchaseOrderNumber">
          <el-input
            v-model="form.purchaseOrderNumber"
            placeholder="请输入采购单号"
          />
        </el-form-item>
        <el-form-item label="品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入品牌" />
        </el-form-item>
        <el-form-item label="已入仓" prop="warehousedQty">
          <el-input v-model="form.warehousedQty" placeholder="请输入已入仓" />
        </el-form-item>
        <el-form-item label="已分配" prop="allocatedQty">
          <el-input v-model="form.allocatedQty" placeholder="请输入已分配" />
        </el-form-item>
        <el-form-item label="待分配" prop="unassignedQty">
          <el-input v-model="form.unassignedQty" placeholder="请输入待分配" />
        </el-form-item>
        <el-form-item label="下单数量" prop="orderQty">
          <el-input v-model="form.orderQty" placeholder="请输入下单数量" />
        </el-form-item>
        <el-form-item label="供应商" prop="supplierName">
          <el-input v-model="form.supplierName" placeholder="请输入供应商" />
        </el-form-item>
        <el-form-item label="采购金额" prop="purchaseAmount">
          <el-input
            v-model="form.purchaseAmount"
            placeholder="请输入采购金额"
          />
        </el-form-item>
        <el-form-item label="在途数量" prop="inTransitQuantity">
          <el-input
            v-model="form.inTransitQuantity"
            placeholder="请输入在途数量"
          />
        </el-form-item>
        <el-form-item label="差异数量" prop="differenceCount">
          <el-input
            v-model="form.differenceCount"
            placeholder="请输入差异数量"
          />
        </el-form-item>
        <el-form-item label="sku" prop="sku">
          <el-input v-model="form.sku" placeholder="请输入sku" />
        </el-form-item>
        <el-form-item label="skc" prop="skc">
          <el-input v-model="form.skc" placeholder="请输入skc" />
        </el-form-item>
        <el-form-item label="spu" prop="spu">
          <el-input v-model="form.spu" placeholder="请输入spu" />
        </el-form-item>
        <el-form-item label="item" prop="item">
          <el-input v-model="form.item" placeholder="请输入item" />
        </el-form-item>
        <el-form-item label="是否新品" prop="isNew">
          <el-select
            v-model="form.isNew"
            placeholder="请输入是否新品"
            filterable
          >
            <el-option
              v-for="dict in dict.type.sys_order_isnew"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="订单状态" prop="orderStatus">
          <el-input v-model="form.orderStatus" placeholder="请输入订单状态" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :visible.sync="openInfo"
      width="1500px"
      append-to-body
      v-if="openInfo"
    >
      <div class="el-dialog-div">
        <component :is="'detail'" :row="row"></component>
      </div>
    </el-dialog>
    <el-dialog
      :visible.sync="openCode"
      width="1500px"
      append-to-body
      v-if="openCode"
    >
      <div class="el-dialog-div">
        <component
          :is="'strategy'"
          @handleStrategy="handleStrategy"
          :showBtn="true"
        ></component>
      </div>
    </el-dialog>

    <el-dialog
      :visible.sync="tagkeyVisible"
      width="600px"
      append-to-body
      v-if="tagkeyVisible"
    >
      <el-card style="margin-bottom: 16px">
        <div slot="header">
          手工分配
          <!-- <span
            >采购单号:{{ this.list[0].purchaseOrderNumber }},sku:{{
              this.list[0].sku
            }}</span
          > -->
        </div>
        <div class="el-dialog-div">
          <el-form :model="formData" :inline="true">
            <el-form-item label="采购单号:">
              {{ this.list[0].purchaseOrderNumber }}
            </el-form-item>
            <el-form-item label="sku:">
              {{ this.list[0].sku }}
            </el-form-item>
            <el-row>
              <el-form-item label="方案编码">
                <el-input v-model="strategyCode" placeholder="请输入方案编码" />
              </el-form-item>
              <el-button
                type="primary"
                plain
                size="mini"
                @click="openCode = true"
              >
                +
              </el-button>
            </el-row>
            <el-row>
              <el-form-item
                style="width: 200px"
                label="店铺名称"
              ></el-form-item>
              <el-form-item
                style="width: 100px"
                label="计划分配数量"
              ></el-form-item>
            </el-row>
            <el-form-item
              v-for="(item, index) in formData.libraryIdList"
              :key="index"
            >
              <!-- <el-input
              style="width: 150px"
              v-model="item.storeName"
              placeholder="店铺名称"
            ></el-input> -->
              <el-select
                v-model="item.storeName"
                clearable
                filterable
                placeholder="店铺"
                style="width: 200px"
              >
                <el-option
                  v-for="item in ShopInfoList"
                  :key="item.comment"
                  :label="`${item.comment}`"
                  :value="item.comment"
                >
                </el-option>
              </el-select>
              <!-- <el-input
              style="width: 150px"
              v-model="item.field_name"
              placeholder="请输入字段值"
            ></el-input> -->
              <el-input
                style="width: 150px"
                v-model="item.plannedAllocationQty"
                placeholder="计划分配数量"
              ></el-input>

              <span
                v-if="formData.libraryIdList.length > 0"
                style="margin-left: 5px"
              >
                <el-button type="primary" size="mini" @click="addLibraryIdList"
                  >新增</el-button
                >
                <el-button
                  type="danger"
                  size="mini"
                  @click="delRow(item, index)"
                  >删除</el-button
                >
              </span>
            </el-form-item>
          </el-form>
        </div>
      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitformData">确 定</el-button>
        <el-button @click="canceltagkeyVisible">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  selectlist,
  getTrigger,
  delTrigger,
  addTrigger,
  updateTrigger,
  syncTriggerTables,
  processandinsert,
  numbers,
} from "@/api/productAllocation/trigger";
// src/views/productAllocation/strategy/index.vue
import strategy from "@/views/productAllocation/strategy/index.vue";
import detail from "@/views/productAllocation/detail/index.vue";
import { JstShopInfo } from "@/api/conf/BiStoreWarehouseBinding";
import timeSearch from "@/views/chart/day/day.vue";
import moment from "moment";
export default {
  name: "Trigger",
  dicts: [
    "sys_product_pp",
    "sys_order_isnew",
    "sys_order_orderstatus",
    "sys_product_arg",
  ],
  components: { detail, timeSearch, strategy },
  props: {
    purchaseOrderNumber: {
      type: String,
      default: null,
    },
    rowskc: {
      type: Object,
      default: () => {},
    },
    rowspu: {
      type: Object,
      default: () => {},
    },
    rowitem: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      queryParamsItotal: 0,
      purchaseDocumentCodeList: [],
      AllList: [],
      openCode: false,
      strategyCode: "",

      list: [],

      dayActive: 365,
      timeArr: [
        { value: "2", label: "昨天" },
        { value: "7", label: "最近7天" },
        { value: "30", label: "最近30天" },
        { value: "365", label: "最近一年" },
        { value: "1", label: "日" },
      ],
      timeGap: { startTime: "", endTime: "" },

      tagkeyVisible: false,
      formData: {
        libraryIdList: [{ storeName: "", plannedAllocationQty: "" }],
      },

      ShopInfoList: [],

      row: {},
      openInfo: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购分配触发表格数据
      triggerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      queryParamsI: {
        pageNum: 1,
        pageSize: 10,
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productGranularity: null,
        orderStatus: null,
        purchaseOrderNumber: null,
        orderType: null,
        brand: null,
        warehousedQty: null,
        allocatedQty: null,
        unassignedQty: null,
        orderQty: null,
        supplierName: null,
        purchaseAmount: null,
        inTransitQuantity: null,
        differenceCount: null,
        sku: null,
        skc: null,
        spu: null,
        item: null,
        isNew: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      value1: "",
      value2: "",
    };
  },
  created() {
    this.getList();
    // numbers({ pageNum: 1, pageSize: 100 }).then((response) => {});
    this.getselectOrderNumber();
    JstShopInfo({ pageNum: 1, pageSize: 100 }).then((response) => {
      this.ShopInfoList = response.rows.map((item) => ({
        columnName: item.shopId,
        comment: item.shopName,
      }));
    });
  },
  watch: {
    purchaseOrderNumber: {
      handler: function (newnew, oldold) {
        this.queryParams.purchaseOrderNumber = this.purchaseOrderNumber;
        this.getList();
      },
      deep: true,
      immediate: true,
    },
    rowskc: {
      handler: function (newnew, oldold) {
        if (JSON.stringify(this.rowskc) != "{}") {
          this.queryParams.skc = this.rowskc.skc;
          this.queryParams.purchaseOrderNumber =
            this.rowskc.purchaseOrderNumber;
          this.getList();
        }
      },
      deep: true,
      immediate: true,
    },
    rowspu: {
      handler: function (newnew, oldold) {
        // if (JSON.stringify(this.spu) != "{}") {
        this.queryParams.spu = this.rowspu.spu;
        this.queryParams.purchaseOrderNumber = this.rowspu.purchaseOrderNumber;
        this.getList();
        // }
      },
      deep: true,
      immediate: true,
    },
    rowitem: {
      handler: function (newnew, oldold) {
        // if (JSON.stringify(this.spu) != "{}") {
        this.queryParams.item = this.rowitem.item;
        this.queryParams.purchaseOrderNumber = this.rowitem.purchaseOrderNumber;
        this.getList();
        // }
      },
      deep: true,
      immediate: true,
    },
  },
  methods: {
    handlepurchaseDocumentCode(row) {
      // this.$emit(
      //   "selectpurchaseDocumentCode",
      //   this.queryParams.purchaseDocumentCode
      // );
      this.$emit("checkpurchaseOrderNumber", row.label);
    },
    //从原来订单选择
    getselectOrderNumber() {
      numbers(this.queryParamsI).then((response) => {
        this.purchaseDocumentCodeList = [];
        response.rows.forEach((item) => {
          this.purchaseDocumentCodeList.push({ label: item });
        });
        this.queryParamsItotal = response.total;
        // this.formData.purchaseDocumentCode = "";
      });
    },
    remoteselectOrderNumber() {
      numbers({ pageNum: 1, pageSize: 5000 }).then((response) => {
        this.AllList = response.rows;
        // response.rows.forEach((item) => {
        //   this.purchaseDocumentCodeList.push({ label: item });
        // });
        // this.queryParamsItotal = response.total;
        // this.formData.purchaseDocumentCode = "";
      });
    },
    tableEdit(row, column, cell, event) {
      // this.checkpurchaseDocumentCode = row.purchaseDocumentCode;
      this.queryParams.purchaseOrderNumber = row.label;
      this.getList();
    },
    handleStrategy(strategyCode) {
      this.strategyCode = strategyCode;
      this.openCode = false;
    },
    changeTime() {
      // console.log(JSON.stringify(this.value2));
      let m1 = moment(this.value2[0]).format("YYYY-MM-DD");
      let m2 = moment(this.value2[1]).format("YYYY-MM-DD");
      // console.log(JSON.stringify(m1));
      // console.log(JSON.stringify(m2));
      this.queryParams.startTime = m1;
      this.queryParams.endTime = m2;
    },
    //时间控件
    changDay(val) {
      this.dayActive = val;
      // this.timeGap.startTime = val[0];
      // this.timeGap.endTime = val[1];
    },
    getDateArr(value) {
      // this.timeGap.startTime = value[0];
      // this.timeGap.endTime = value[1];
      // console.log(JSON.stringify(this.timeGap));

      this.queryParams.startTime = value[0];
      this.queryParams.endTime = value[1];

      this.getList();

      this.dayActive = 1;
    },
    handlepurchaseOrderNumber() {
      if (this.list.length != 1) {
        this.$modal.msgError("只勾选一条记录");
        return;
      }
      this.$emit("checkpurchaseOrderNumber", this.list[0].purchaseOrderNumber);
      // this.
    },

    submitformData() {
      if (!this.formData.libraryIdList[0].storeName) {
        this.$modal.msgError("至少输入一个店铺");
        return;
      }
      if (!this.formData.libraryIdList[0].plannedAllocationQty) {
        this.$modal.msgError("请设置计划分配数量");
        return;
      }
      // let obj = {
      //   strategyCode: this.row.strategyCode,
      //   businessType: "priority",
      //   allocationPurchaseConfShopList: this.formData.libraryIdList,
      // };
      this.formData.libraryIdList.forEach((item) => {
        item.purchaseOrderNumber = this.list[0].purchaseOrderNumber;
        item.productCode = this.list[0].sku;
        item.strategyCode = this.strategyCode;
      });
      processandinsert(this.formData.libraryIdList).then((response) => {
        if (response.code == 200) {
          this.$modal.msgSuccess(response.msg);
          this.tagkeyVisible = false;
        }
        // else {
        //     this.$modal.msgError("设置失败");
        //     this.tagkeyVisible = false;
        //   }
      });
    },
    opentagkey() {
      // listShop({
      //   strategyCode: this.row.strategyCode,
      //   businessType: "priority",
      // }).then((response) => {
      //   let list = [{ storeName: "", plannedAllocationQty: "" }];
      //   if (response.rows.length > 0) {
      //     list = [];
      //     response.rows.forEach((item) => {
      //       list.push({
      //         storeName: item.storeName,
      //         plannedAllocationQty: item.plannedAllocationQty,
      //       });
      //     });
      //   }
      //   // this.cols
      //   this.formData.libraryIdList = list;
      // });
      if (this.list.length != 1) {
        this.$modal.msgError("只勾选一条记录");
        return;
      }

      this.tagkeyVisible = true;
    },
    // 取消按钮
    canceltagkeyVisible() {
      this.tagkeyVisible = false;
    },
    //添加库存名称
    addLibraryIdList() {
      this.formData.libraryIdList.push({
        storeName: "",
        plannedAllocationQty: "",
      });
    },
    //库存名称删除
    delRow(item, index) {
      if (this.formData.libraryIdList.length > 0) {
        this.formData.libraryIdList.splice(index, 1);
      } else {
        this.$message.warning("至少选择一项");
      }
    },

    getBiCommentSecondDwdList(row) {
      this.row = row;

      this.openInfo = true;
      return;
      let obj = {
        commentFirstId: row.id,
      };
      listBiCommentSecondDwd(obj).then((response) => {
        this.BiCommentSecondDwdList = response.rows;
      });
    },
    // 打开加载层
    openLoading() {
      this.$modal.loading("正在加载数据，请稍候！");
    },
    /** 刷新缓存按钮操作 Warning*/
    handleRefresh() {
      // this.$modal.msgWarning("数据正在同步，请稍候");

      //   const obj = {
      //     startTime: this.timeGap.startTime,
      //     endTime: this.timeGap.endTime,
      //   };
      this.openLoading();
      syncTriggerTables(this.queryParams).then(() => {
        this.$modal.closeLoading();
        this.$modal.msgSuccess("同步数据成功");
        this.getList();
        // this.$store.dispatch('dict/cleanDict');
      });
    },
    /** 查询采购分配触发列表 */
    getList() {
      this.loading = true;
      selectlist(this.queryParams).then((response) => {
        this.triggerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        productGranularity: null,
        orderStatus: null,
        purchaseOrderNumber: null,
        orderType: null,
        brand: null,
        warehousedQty: null,
        allocatedQty: null,
        unassignedQty: null,
        orderQty: null,
        supplierName: null,
        purchaseAmount: null,
        inTransitQuantity: null,
        differenceCount: null,
        sku: null,
        skc: null,
        spu: null,
        item: null,
        isNew: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams = {};
      this.value2 = "";
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.list = selection.map((item) => item);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加采购分配触发";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getTrigger(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改采购分配触发";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateTrigger(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTrigger(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除采购分配触发编号为"' + ids + '"的数据项？')
        .then(function () {
          return delTrigger(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "productAllocation/trigger/export",
        {
          ...this.queryParams,
        },
        `trigger_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
